package com.mbc.rmt.manager;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.DBManager;
import com.mbc.common.dao.DBManagerFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Log;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.ACGroupDetailDAO;
import com.mbc.rmt.dao.COACurrencyDAO;
import com.mbc.rmt.dao.COADAO;
import com.mbc.rmt.dao.IAIMSCOACurrencyDAO;
import com.mbc.rmt.dao.IAIMSCOADAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.COADataList;
import com.mbc.rmt.entities.ACGroupDetail;
import com.mbc.rmt.entities.COA;
import com.mbc.rmt.entities.COACurrency;
import com.mbc.rmt.entities.COARpt;


public class COAMgr extends AbstractMgr {

	private COADAO coaDAO;
	private COACurrencyDAO coacurrencyDAO;
	private ACGroupDetailDAO acgroupdetailDAO;
	private LogDAO logDAO;
	
	private IAIMSCOADAO iaimscoaDAO;
	private IAIMSCOACurrencyDAO iaimscoacurrencyDAO;

	public COAMgr() {
	}

	public COAMgr(String sessionId) {
		this.sessionId = sessionId;
	}

	public void create(COA entity) throws Exception {
		DBManager l_DBManger = DBManagerFactory.getDBManager(sessionId, DBManager.LINKED_IAIMS);
		openConnection();
		try {
			l_DBManger.beginTransaction();
			beginTransaction();
			
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());
			coacurrencyDAO = RemittanceDAOFactory.getCOACurrencyDAO(dbManager.getConnection());
			
			iaimscoaDAO = RemittanceDAOFactory.getIAIMSCOADAO(l_DBManger.getConnection());
			iaimscoacurrencyDAO = RemittanceDAOFactory.getIAIMSCOACurrencyDAO(l_DBManger.getConnection());

			entity.setCOAKey(KeyGenerator.generateKey(getConnection()));
			
			coaDAO.create(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("COA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getCOAKey());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
			COA l_entity = iaimscoaDAO.findCOAByCode(entity.getAC_ID(), entity.getBranchId());
			if(l_entity == null)
				iaimscoaDAO.create(entity);

			for (COACurrency l_infoCOACurrency : entity.getCoacurrencyList()) {
				l_infoCOACurrency.setCoakey(entity.getCOAKey());
				l_infoCOACurrency.setCoacurrencykey(KeyGenerator
						.generateKey(getConnection()));
				coacurrencyDAO.create(l_infoCOACurrency);
				
				if(l_entity == null)
					iaimscoacurrencyDAO.create(l_infoCOACurrency);
				
				l_logentity.setTablename("COACurrency");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(l_infoCOACurrency.getCoacurrencykey());
				l_logentity.setUserid(l_infoCOACurrency.getUserid());
				l_logentity.setLogstatus(Log.__SAVE_DATA);
				logDAO.getData(l_logentity);
			}
			
			commintTransaction();
			l_DBManger.commitTransaction();

		} catch (Exception e) {
			l_DBManger.rollBackTransaction();
			rollbackTransaction();
			throw e;
		} finally {
			l_DBManger.closeConnection();
			closeConnection();
		}
	}

	private long blankACGroupDetailCreate(COA entity) throws Exception {
		long key = KeyGenerator.generateKey(getConnection());

		ACGroupDetail ac = new ACGroupDetail();

		ac.setAcgroupdetailId(key);
		ac.setAcgroup(entity.getACGroup());
		ac.setActype(entity.getACType());
		ac.setAcgroupdetailCode("_");
		ac.setDescription("Blank Description");

		acgroupdetailDAO = RemittanceDAOFactory.getACGroupDetailDAO(dbManager
				.getConnection());
        acgroupdetailDAO.create(ac);
		return key;
	}
	
	public void update(COA entity) throws Exception {
		DBManager l_DBManger = DBManagerFactory.getDBManager(sessionId, DBManager.LINKED_IAIMS);
		openConnection();
		try {
			l_DBManger.beginTransaction();
			beginTransaction();
			
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());
			coacurrencyDAO = RemittanceDAOFactory.getCOACurrencyDAO(dbManager.getConnection());
			
			iaimscoaDAO = RemittanceDAOFactory.getIAIMSCOADAO(l_DBManger.getConnection());
			iaimscoacurrencyDAO = RemittanceDAOFactory.getIAIMSCOACurrencyDAO(l_DBManger.getConnection());

		/*	if (entity.getACGroupDetailKey() == 0) {
				entity.setACGroupDetailKey(blankACGroupDetailCreate(entity));
			}*/

			coaDAO.update(entity);
			coacurrencyDAO.delete(entity.getCOAKey());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("COA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getCOAKey());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
			iaimscoaDAO.update(entity);
			iaimscoacurrencyDAO.delete(entity.getCOAKey());
			
			for (COACurrency l_infoCOACurrency : entity.getCoacurrencyList()) {
				l_infoCOACurrency.setCoakey(entity.getCOAKey());
				l_infoCOACurrency.setCoacurrencykey(KeyGenerator.generateKey(getConnection()));
				
				coacurrencyDAO.create(l_infoCOACurrency);
				iaimscoacurrencyDAO.create(l_infoCOACurrency);
				
				logDAO = CommonDAOFactory.getLogDAO(getConnection());
				l_logentity.setTablename("COACurrency");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(l_infoCOACurrency.getCoacurrencykey());
				l_logentity.setUserid(l_infoCOACurrency.getUserid());
				l_logentity.setLogstatus(Log.__UPDATE_DATA);
				logDAO.getData(l_logentity);
			}
			
			commintTransaction();
			l_DBManger.commitTransaction();
		} catch (Exception e) {
			l_DBManger.rollBackTransaction();
			rollbackTransaction();
			throw e;
		} finally {
			l_DBManger.closeConnection();
			closeConnection();
		}
	}

	public void delete(long key, long userid) throws Exception {
		DBManager l_DBManger = DBManagerFactory.getDBManager(sessionId, DBManager.LINKED_IAIMS);
		openConnection();
		try {
			l_DBManger.beginTransaction();
			beginTransaction();
			
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());
			coacurrencyDAO = RemittanceDAOFactory.getCOACurrencyDAO(dbManager.getConnection());
			
			iaimscoaDAO = RemittanceDAOFactory.getIAIMSCOADAO(l_DBManger.getConnection());
			iaimscoacurrencyDAO = RemittanceDAOFactory.getIAIMSCOACurrencyDAO(l_DBManger.getConnection());
			
			coacurrencyDAO.delete(key);
			coaDAO.delete(key);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("COA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
			iaimscoacurrencyDAO.delete(key);
			iaimscoaDAO.delete(key);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			l_logentity.setTablename("COACurrency");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
			commintTransaction();
			l_DBManger.commitTransaction();
		} catch (Exception e) {
			l_DBManger.rollBackTransaction();
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
			l_DBManger.closeConnection();
		}
	}

	public COA find(long key) throws Exception {
		openConnection();
		try {
			
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());
			
			return coaDAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public COA findCOAByCode(String code,long branchId) throws Exception {
		openConnection();
		try {
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());
			return coaDAO.findCOAByCode(code,branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public List<COA> findAll() throws Exception {
		openConnection();
		try {
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());
			return coaDAO.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public COADataList findCOAs(COA cri) throws Exception {
		openConnection();
		try {
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());
			return coaDAO.findCOAs(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public ArrayList<COACurrency> getCOACurrencyList(long coakey)
			throws Exception {
		openConnection();
		try {
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());
			return coaDAO.getInfoList(coakey);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public COADataList getCOAList(COARpt info) throws Exception {
		openConnection();
		try {
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());
			return coaDAO.getCOAList(info);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public JasperPrint prepareCOAReport(COARpt info) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			coaDAO = RemittanceDAOFactory.getCOADAO(dbManager.getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(
					coaDAO.prepareCOAReport(info));

			JasperReport l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH
							+ "reports/COARpt.jasper");
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("company", "MBC");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
}
